//
//  ViewController.m
//  UI05_scrollView
//
//  Created by Hanlingyu on 16/7/20.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"

// 宏定义 命名两种方式
// 宏就是系统编译时, 代码的替换(预编译指令)
#define kScreenBounds [UIScreen mainScreen].bounds
// 一. 全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 二. k + 驼峰命名
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:scrollView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, kScreenWidth - 20, 100);
    button.backgroundColor = [UIColor redColor];
//    [button setTitle:<#(nullable NSString *)#> forState:<#(UIControlState)#>];
//    [button setImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>];
//    [button setBackgroundImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]
//    [button addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>];
    [scrollView addSubview:button];
    
    // 1. 默认 CGSizeZero CGSizeMake(0, 0)
    // contentSize 设置可以滚动的范围
    scrollView.contentSize = CGSizeMake(kScreenWidth * 4, 1000);
    // 2. 默认 CGPointZero
    // contentOffset 偏移量, 滚动的位置
    // 不设置contentSize, contentOffset也可以改变
    // 问题: 改变contentOffset什么变了?
    // 改变contentOffset, 实质上是对scrollView的bounds进行修改
    NSLog(@"offset 改变前 scrollView frame is: %@", NSStringFromCGRect(scrollView.frame));
    scrollView.contentOffset = CGPointMake(0, 500);
    
    [UIImage imageNamed:]
    
    // 3. contentInset
    // 加边距, 滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4. directionalLockEnabled
    // 锁定垂直或水平滚动(45°角, 锁会失效)
    scrollView.directionalLockEnabled = YES;
    // 5. bounces回弹效果
//    scrollView.bounces = NO;
    // 6. alwaysBounceVertical 在没有垂直方向的滚动范围时, 保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    // 7. alwaysBounceHorizontal 在没有水平方向的滚动范围时, 保证水平方向的回弹效果
    scrollView.alwaysBounceHorizontal = NO;
    // 8. pagingEnabled 按页滚动
    scrollView.pagingEnabled = YES;
    // 9. scrollEnabled 滚动视图能否滚动, 默认YES(可以滚), 默认NO(不可以滚)
    scrollView.scrollEnabled = YES;
    // 10. 是否显示垂直/水平的滚动指示器, 默认YES
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    // 11. 滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    // 12. 指示器的Style 滚动指示器的颜色(深色背景, 浅色背景)
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    // 13.相关用户操作
//    scrollView.decelerating 用户是否开始拖动
//    scrollView.dragging 用户是否正在拖动
//    scrollView.decelerating 是否减速中
    // 14.滚动驶入 双击状态栏收拾是否存在 默认YES
    scrollView.scrollsToTop = NO;
    // 15.代理
    scrollView.delegate = self;
    // 16.键盘消失模式 (3种)
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    
    
    // 测试:
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, kScreenWidth - 20, 200)];
    textField.backgroundColor = [UIColor redColor];
    [scrollView addSubview:textField];
    
    // 17.缩放系数
    // 如果想进行视图缩放, 必须实现viewForZoomingInScrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
    
    
    
}
// 与其想要缩放的子视图绑定, scrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return scrollView.subviews.firstObject;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"滚动中");
    // tracking 用户触摸
    if (scrollView.tracking) {
        NSLog(@"正在拖动滚动");
    }
    
    // dragging 用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"");
    }
    
    // decelerating 用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
}
// 结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

}
// 开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
}
// 结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
